<?php

namespace app\api\controller;

// use app\api\controller\Tpmecms;
use app\api\controller\Ltder;
use think\Db;

/**
 * 客户
 * 
 */
class Clients extends Ltder
{

    protected $db = 'clients';
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // /**
    //  * 测试方法
    //  *
    //  * @ApiTitle    (测试名称)
    //  * @ApiSummary  (测试描述信息)
    //  * @ApiMethod   (POST)
    //  * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
    //  * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
    //  * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
    //  * @ApiParams   (name="name", type="string", required=true, description="用户名")
    //  * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
    //  * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
    //  * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
    //  * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
    //  * @ApiReturn   ({
    //      'code':'1',
    //      'msg':'返回成功'
    //     })
    //  */
    // public function test()
    // {
    //     $this->success('返回成功', $this->request->param());
    // }


    /**
     * 添加我的客户
     * 
     * @param string $name 姓名
     * @param string $wechat 微信号
     * @param string $tel 电话
     * @param string $sex 性别:1=男,2=女,0=未知
     * @param string $house_status  房屋类型:1=租房,2=买房,3=卖房,5=未知
     * @param string $area_size 意向面积大小
     * @param string $area 意向区域
     * @param string $ps 备注
     * @param string $house_type 意向户型
     * @param string $down_pay 首付预算
     * 
     * @return void
     */
    public function add_clients()
    {
       
        $data['tel'] = $this->request->param('tel','');
        $data['uid'] = $this->auth->id;

        $ltder = $this->GetLtdUserData($this->auth->id);

        $data['ltd_id'] = $ltder['ltd_id'];
        $data['admin_id'] = $ltder['admin_id'];

        $res = Db::name('clients')->where($data)->find();
        if($res){
            $this->error('已有该客户的手机号');
        }

       
        $data['name'] = $this->request->param('name','');

        

        $data['sex'] = $this->request->param('sex','0');
        // $data['house_status'] = $this->request->param('house_status','');
        $data['ps'] = $this->request->param('ps','');
        $data['area'] = $this->request->param('area','');

        $data['down_pay'] = $this->request->param('down_pay','');
        $data['house_type'] = $this->request->param('house_type','');


        

        $data['time'] = time();

        $res = Db::name($this->db)->insert($data);
        if($res){
            $this->success('添加成功');
        }
    }

    /**
     * 获取我的客户列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_my_clients_list($page=1,$limit=10)
    {
        $where['pid'] =$this->auth->id;
        $where['success_status'] = '1';
        $list = Db::name('clients')
                ->where($where)
                ->where('status','1')

                ->order('time desc')
                ->page($page,$limit)
                ->select();
        foreach ($list as $key => &$val) {
             
            $needs = Db::name('clients_need')->where('clients_id',$val['id'])->select();
            foreach ($needs as $key2=> &$val2) {
                $val2['goods'] = $this->GetGoodsData2($val2['goods_id']);
            }

            $val['clients_needs'] = $needs;
        }
        $this->success('ok',$list);
    }


    /**
     * 获取某个客户列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param integer $clients_id 客户id
     * @return void
     */
    public function get_clients_need_list2($page=1,$limit=10,$clients_id)
    {

        $where['clients_id'] = $clients_id;
        $list = Db::name('clients_need')->where($where)->select();
        foreach ($list as $key=> &$val) {
            $val['goods'] = $this->GetGoodsData2($val['goods_id']);
        }

      
        $this->success('ok',$list);
    }


    /**
     * 添加跟进内容🌏 
     *
     * @param string $clients_need_id 跟进id
     * @param string $str 内容
     * @return void
     */
    public function add_ps($clients_need_id,$str = null)
    {
        $clients_need = Db::name('clients_need')->find($clients_need_id);
        if(!$clients_need){
            $this->error('没有该记录');
        }elseif($clients_need['pid']!=$this->auth->id){
            $this->error('非本人客户');
        }
        $ps = $clients_need['ps'] .'<br>'.date('Y-m-d H:i:s').': '.$str;
        $res = Db::name('clients_need')->where('id',$clients_need_id)->update(['ps'=>$ps]);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }

        
    }


    /**
     * 修改记录状态:1=未成交,2=成交🌏 
     *
     * @param string $clients_need_id 跟进id
     * @param string $status 状态:1=未成交,2=成交
     * @return void
     */
    public function update_status($clients_need_id,$status='1')
    {
        $clients_need = Db::name('clients_need')->find($clients_need_id);
        if(!$clients_need){
            $this->error('没有该记录');
        }elseif($clients_need['pid']!=$this->auth->id){
            $this->error('非本人客户');
        }

        // $ps = $clients_need['ps'] .'<br>'.date('Y-m-d H:i:s').': '.$str;
        $res = Db::name('clients_need')->where('id',$clients_need_id)->update(['status'=>$status]);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }        
    }

    
    /**
     * 修改客户记录状态:1=未成交,2=成交🌏 
     *
     * @param string $clients_id 客户id
     * @param string $status 成交状态:1=待成交,2已成交
     * @return void
     */
    public function update_clients_success_status($clients_id,$status='1')
    {
        $clients = Db::name('clients')->find($clients_id);
        if(!$clients){
            $this->error('没有该记录');
        }elseif($clients['pid']!=$this->auth->id){
            $this->error('非本人客户');
        }

        // $ps = $clients['ps'] .'<br>'.date('Y-m-d H:i:s').': '.$str;
        $res = Db::name('clients')->where('id',$clients_id)->update(['success_status'=>$status]);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }        
    }


    /**
     * 修改跟进记录:1=持续跟进,2=放弃跟进🌏 
     *
     * @param string $clients_need_id 跟进id
     * @param string $now_status 跟进状态:1=持续跟进,2=放弃跟进
     * @return void
     */
    public function update_now_status($clients_need_id,$now_status='1')
    {
        $clients_need = Db::name('clients_need')->find($clients_need_id);
        if(!$clients_need){
            $this->error('没有该记录');
        }elseif($clients_need['pid']!=$this->auth->id){
            $this->error('非本人客户');
        }

        // $ps = $clients_need['ps'] .'<br>'.date('Y-m-d H:i:s').': '.$str;
        $res = Db::name('clients_need')->where('id',$clients_need_id)->update(['now_status'=>$now_status]);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }        
    }


    /**
     * 编辑客户信息
     *
     * @param int $id id
      * @return void
     */
    public function edit_clients($id)
    {
        $params = $this->request->param(); 
        // halt($params);
        $data = $this->get_field_arr($params,['name'
                                        ,'wechat'
                                        ,'tel'
                                        ,'sex'

                                        ,'goods_ids'
                                        ,'room_number'
                                        ,'money'
                                        ,'size'
                                        ,'jf_time'
                                        ],true);
                                 
        if(isset($data['money']) && isset($data['size'])){
            $data['price'] = round($data['money']/$data['size'],2);
        }
        $res = Db::name($this->db)->where('pid',$this->auth->id)->where('id',$id)->update($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除客户
     *
     * @param int $id id
     * @return void
     */
    public function del_clients($id)
    {
        $res = Db::name($this->db)->where('pid',$this->auth->id)->where('id',$id)->find();
        if($res){
            $this->DelClients($id);
            $this->success('删除成功');
        }else{
            $this->error('没找到');
        }
    }




    /**
     * 获取成交客户列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $success_status 状态:1=未成交,2=成交
     * @return void
     */
    public function get_success_clients_list($page=1,$limit=10,$success_status='2')
    {
        $where = null;
        if($success_status){
            $where['success_status'] = $success_status;
        }

        $list = Db::name('clients')
                ->where('pid',$this->auth->id)
                ->where($where)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
            
        foreach ($list as $key => &$val) {
            // halt($val['goods_ids']);
            $val['goods'] = $this->GetGoodsIdsData($val['goods_ids']); 
            // $val['clients'] = $this->GetClients($val['clients_id']);
            // $val['tags'] = $this->TagIdsToArray($val['tag_ids']);
        }

        $this->success('ok',$list);
    }

    // /**
    //  * Undocumented function
    //  *
    //  * @return void
    //  */
    // public function add_clients_data()
    // {
    //     # code...
    // }

}
